<?php

namespace App\Admin\Controllers;

use App\Models\ShopApply;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ShopApplyController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('申请成为商家');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('申请成为商家');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ShopApply::class, function (Grid $grid) {
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('user.name', '名字');
                $filter->like('user.mobile', '联系方式');
                $filter->equal('status','状态')->radio([
                    0    => '等待中',
                    1    => '已通过',
                    2    => '已拒绝',
                ]);
            });
            $grid->id('ID')->sortable();
            $grid->column('user.name', '名字');
            $grid->column('user.mobile', '联系方式');
            $grid->status('状态')->display(function ($status){
                switch ($status){
                    case '0':
                        return "<span style='color:#0b00ff'>等待中</span>";
                        break;
                    case '1':
                        return "<span style='color:#01B488'>已通过</span>";
                        break;
                    case '2':
                        return "<span style='color:#ff000d'>已拒绝</span>";
                        break;
                }
            });
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ShopApply::class, function (Form $form) {
            $form->saved(function (Form $form) {
                //通过
                if($form->status == 1){
                    User::where('id',$form->model()->user_id)->update(['role'=>1]);
                }
                //拒绝
                if($form->status == 2){
                    User::where('id',$form->model()->user_id)->update(['role'=>0]);
                }
            });
            $form->display('id', 'ID');
            $form->display('user.name', '名字');
            $form->display('user.mobile', '联系方式');
            $form->radio('status','审核状态')->options(['1'=> '通过',2 => '拒绝']);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
